package com.example.localareanetworkchat.fragment.home

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import androidx.recyclerview.widget.GridLayoutManager
import com.example.localareanetworkchat.R
import com.example.localareanetworkchat.fragment.MainViewModel
import com.example.localareanetworkchat.fragment.home.adapter.ChatGroupMemberListAdapter
import kotlinx.android.synthetic.main.fragment_chat_group_info.*


class ChatGroupInfoFragment : Fragment() {

    var viewModel: MainViewModel ?= null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_chat_group_info, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        requireActivity().findViewById<Toolbar>(R.id.toolbar).menu.clear()
        requireActivity().findViewById<Toolbar>(R.id.toolbar).setNavigationIcon(R.drawable.ic_baseline_keyboard_backspace_24)
        val navController = Navigation.findNavController(requireView())
        requireActivity().findViewById<Toolbar>(R.id.toolbar).setNavigationOnClickListener {
            navController.navigate(R.id.messageFragment)
        }

        viewModel = ViewModelProvider(requireActivity(), ViewModelProvider.AndroidViewModelFactory(requireActivity().application)).get(
            MainViewModel::class.java)

        val chatGroup = viewModel?.chatGroup
        tv_chat_group_info_name.text = chatGroup?.groupName

        val chatGroupMemberAdapter = ChatGroupMemberListAdapter(requireContext())
        rv_chat_group_info_member.layoutManager = GridLayoutManager(requireContext(), 5, GridLayoutManager.VERTICAL, false)
        rv_chat_group_info_member.adapter = chatGroupMemberAdapter

        viewModel?.queryAllChatGroupUserByChatGroupId(chatGroup!!.chatGroupId)?.observe(requireActivity(), {
            chatGroupMemberAdapter.submitList(it)
        })

        tv_chat_group_info_delete.setOnClickListener {

            val dialogBuilder = AlertDialog.Builder(requireContext())
            dialogBuilder.setTitle("退群提示")
            dialogBuilder.setMessage("是否退出群：" + chatGroup?.groupName)

            dialogBuilder.setPositiveButton("确定") { _, _ ->
                viewModel?.deleteChatGroup(viewModel?.user!!, chatGroup!!)
                Navigation.findNavController(it).navigate(R.id.homeFragment)
            }
            dialogBuilder.setNegativeButton("取消") { _, _ ->
            }
            dialogBuilder.setCancelable(true)
            dialogBuilder.show()


        }
    }


}